package com.cloudxingkong.dianyinke.Student.Ui;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.cloudxingkong.dianyinke.NewTeacher.Ui.Login;
import com.cloudxingkong.dianyinke.NewTeacher.Utils.CookieHelper;
import com.cloudxingkong.dianyinke.NewTeacher.Utils.SysExitUtil;
import com.cloudxingkong.dianyinke.R;
import com.cloudxingkong.dianyinke.Student.bean.StudentMessage;
import com.cloudxingkong.dianyinke.Uri.studentUri.StudentUri;
import com.cloudxingkong.dianyinke.Uri.teacherUri.ThisUri;
import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 个人中心Activity
 */
public class PersonalCenter extends AppCompatActivity {


	private TextView grzx_tv_name;
	private CircleImageView grzx_icon;
	private SharedPreferences sharedPreferences;
	private String sessionID;

	@Override
	protected void onCreate (Bundle savedInstanceState) {
		super.onCreate (savedInstanceState);
		setContentView (R.layout.activity_personal_center);
		SysExitUtil.activityList.add (this);
		sharedPreferences = getSharedPreferences ("data_dyk", 0);
		init ();
		loadData ();
	}


	private void loadData () {

		RequestParams params = new RequestParams (ThisUri.SERVER_YUMING + StudentUri.STUDNETMESSAGE);
		params.addHeader ("cookie", sessionID);
		x.http ().get (params, new Callback.CommonCallback<String> () {
			@Override
			public void onSuccess (String result) {
				Message message = handler.obtainMessage ();
				message.what = 1;
				message.obj = result;
				handler.sendMessage (message);
			}

			@Override
			public void onError (Throwable ex, boolean isOnCallback) {

			}

			@Override
			public void onCancelled (CancelledException cex) {

			}

			@Override
			public void onFinished () {

			}
		});
	}

	private Handler handler = new Handler () {
		@Override
		public void handleMessage (Message msg) {
			super.handleMessage (msg);
			String state = (String) msg.obj;
			if (state != null) {
				Gson gson = new Gson ();
				StudentMessage studentMessage = null;
				studentMessage = gson.fromJson (state, StudentMessage.class);
				String studentname = studentMessage.getObj ().getMemname () + "";
				String studentpic = studentMessage.getObj ().getAvatar () + "";
				grzx_tv_name.setText (studentname + "");

				ImageLoader.getInstance ().displayImage (studentpic, grzx_icon);
			}
		}

	};

	private void init () {
		grzx_tv_name = (TextView) findViewById (R.id.grzx_tv_name);
		//grzx_tv_phone = (TextView) findViewById(R.id.grzx_tv_phone);
		grzx_icon = (CircleImageView) findViewById (R.id.grzx_icon);

		sessionID = CookieHelper.getSessionID (sharedPreferences);
		;
	}

	@Override
	protected void onActivityResult (int requestCode, int resultCode, Intent data) {
		if (requestCode == 2) {
			if (resultCode == 3) {
				loadData ();
			}
		}
		super.onActivityResult (requestCode, resultCode, data);
	}

	/*
		老师头像
		 */
	public void grzx_icons (View view) {
		Intent intent = new Intent (this, MaterialUpload.class);
		startActivityForResult (intent, 2);
	}

	/*
	我的预约
	 */
	public void my_yuyue (View view) {
		Intent intent = new Intent (this, MyAppointment.class);
		startActivity (intent);
	}

	/*
	我的积分
	 */
	public void my_jifen (View view) {
		Intent jifen = new Intent (this, IntegralDetails.class);
		startActivity (jifen);
	}

	/*
	我的兑换
	 */
	public void my_duihuan (View view) {
		Intent duihuan = new Intent (this, MyChange.class);
		startActivity (duihuan);
	}

	/*
	我的地址
	 */
	public void my_dizhi (View view) {
		Intent dizhi = new Intent (this, MySite.class);
		startActivity (dizhi);
	}

	//退出
	public void activity_Login (View view) {
		AlertDialog.Builder builder = new AlertDialog.Builder (this);
		builder.setTitle ("提示！")
				.setMessage ("确定退出当前登录吗？")
				.setNegativeButton ("取消", null)
				.setPositiveButton ("确定", new DialogInterface.OnClickListener () {
					private ProgressDialog progressDialog;

					@Override
					public void onClick (DialogInterface dialog, int which) {
						progressDialog = ProgressDialog.show (PersonalCenter.this, null, "正在操作...", true, false);
						RequestParams params = new RequestParams (ThisUri.SERVER_YUMING + ThisUri.SIGN_OUT);
						params.addHeader ("cookie", sessionID);
						x.http ().post (params, new Callback.CommonCallback<String> () {
							@Override
							public void onSuccess (String result) {
								progressDialog.dismiss ();
								Intent intent = new Intent (PersonalCenter.this, Login.class);
								intent.putExtra ("msg", "empty");
								startActivity (intent);
								Log.e ("test", "有多少activity " + SysExitUtil.activityList.size ());
								SysExitUtil.exit ();
							}

							@Override
							public void onError (Throwable ex, boolean isOnCallback) {
								progressDialog.dismiss ();
								Intent intent = new Intent (PersonalCenter.this, Login.class);
								intent.putExtra ("msg", "empty");
								startActivity (intent);
								Log.e ("test", "有多少activity " + SysExitUtil.activityList.size ());
								SysExitUtil.exit ();
							}

							@Override
							public void onCancelled (CancelledException cex) {

							}

							@Override
							public void onFinished () {

							}
						});
					}
				}).create ().show ();
	}

	public void backs (View view) {
		finish ();
	}
}
